#include "EntityEnemy.h"
#include "Visualisation.h"
#include "WorldModel.h"
#include "AI.h"
#include "Sound.h"

CEntityEnemy::CEntityEnemy(D3DXVECTOR3 initialPosition, float initialPitch, float initialYaw, float initialRoll, int graphicIndex, 
						   float speed, bool collision, int indexSound, int durationSound, float damage, float offsetY):CWorldEntity(),
						   m_previousLengthBetweenLookVectors(-1), m_positiveWayOfTurning(true), m_startTimeInterpolation(0), 
						   m_durationInterpolation(500), m_startPositionInterpolation(initialPosition), 
						   m_endPositionInterpolation(initialPosition), m_offsetY(offsetY), m_offsetYaw((float)3.14)
{ 
	m_position=initialPosition;
	m_previousPosition=initialPosition;
	m_pitch=initialPitch;
	m_yaw=initialYaw; 
	m_roll=initialRoll;
	m_previousPitch=m_pitch;
	m_previousYaw=m_yaw;
	m_previousRoll=m_roll;
	m_gfxIndex=graphicIndex;
	m_speed=speed;
	m_side=eSideEnemy;
	m_detectCollision=collision;
	calculBoundingBox();
	calculOBBBoundingBox();
	calculBoundingBoxIntoWorldSpace();

	m_collisionCost=damage;

	m_lastTimeSoundPlayed=0; 
	m_indexSound=indexSound; 
	m_durationSound=durationSound;
}

CEntityEnemy::~CEntityEnemy(void){}

bool CEntityEnemy::isArrived()
{
	if(m_position.x==m_endPositionInterpolation.x && m_position.z==m_endPositionInterpolation.z)
		return true;

	return false;
}

void CEntityEnemy::doInterpolation(void)
{
	if(m_startTimeInterpolation==0)
		m_startTimeInterpolation=timeGetTime();

	float currentTime=(timeGetTime()-m_startTimeInterpolation)/m_durationInterpolation;

	if(currentTime>=1)
		{
			m_position=m_endPositionInterpolation;
			m_startTimeInterpolation=0;
			m_startPositionInterpolation=m_endPositionInterpolation;
		}
		else
			m_position=m_startPositionInterpolation+currentTime*(m_endPositionInterpolation-m_startPositionInterpolation);
}

void CEntityEnemy::doUpdate(void)
{
	//step variable
	float stepRotation=(float)0.2;

	//find the position of the player and calcul the new look
	D3DXVECTOR3 goal;
	AI->findGoal(&goal);
	D3DXVECTOR3 newLook=goal-m_position;
	D3DXVec3Normalize(&newLook, &newLook);
	
	//calcul the current look
	D3DXVECTOR3 look(0, 0, 1);
	D3DXVECTOR3 up(0, 1, 0);
	D3DXMATRIX yawMatrix;
	D3DXMatrixRotationAxis(&yawMatrix, &up, m_yaw);
	D3DXVec3TransformCoord(&look, &look, &yawMatrix);
	D3DXVec3Normalize(&look, &look);

	//calcul the distance between the two look
	D3DXVECTOR3 vectorDistance=newLook-look;
	float lengthDistance=D3DXVec3Length(&vectorDistance);

	//turn the enemy till he faces the player
	if(lengthDistance>=0.1)
	{
		//initialisation of m_previousLength
		if(m_previousLengthBetweenLookVectors==-1)
			m_previousLengthBetweenLookVectors=lengthDistance;
		else
		{
			//if we're turning on the wrong way
			if(m_previousLengthBetweenLookVectors<lengthDistance)
				m_positiveWayOfTurning=!m_positiveWayOfTurning;
		}

		//calcul the new yaw
		if(m_positiveWayOfTurning)
			m_yaw+=stepRotation;
		else
			m_yaw-=stepRotation;

		m_previousLengthBetweenLookVectors=lengthDistance;
	}
		
	if(isArrived())
		m_endPositionInterpolation+=m_speed*look;

	doInterpolation();

	//calcul the new Y
	m_position.y=(float)(WORLDMODEL->terrainFollowing(&m_position)+m_offsetY);
	
	//calcul the collision rectangle and collision
	calculBoundingBoxIntoWorldSpace();

	CWorldEntity* collisionEntity=WORLDMODEL->checkCollision(this);
	if(collisionEntity!=0)//if collision
	{
			m_position=m_previousPosition;
			m_endPositionInterpolation=m_position;
			m_startTimeInterpolation=0;
			m_pitch=m_previousPitch;
			m_yaw+=(float)(3.14/20);
			m_roll=m_previousRoll;
	}
	else
	{
		m_previousPosition=m_position;
		m_previousPitch=m_pitch;
		m_previousYaw=m_yaw;
		m_previousRoll=m_roll;
	}
	
	if(m_lastTimeSoundPlayed==0)
	{
		SOUND->playSound(m_indexSound);
		m_lastTimeSoundPlayed=timeGetTime();
	}

	if(timeGetTime()-m_lastTimeSoundPlayed>=m_durationSound)
	{
		SOUND->playSound(m_indexSound);
		m_lastTimeSoundPlayed=timeGetTime();
	}
}

void CEntityEnemy::doRender(void)
{
	VIS->displayModel(m_gfxIndex, m_position, m_pitch, m_yaw+m_offsetYaw, m_roll);
}